#include "FileReader.hxx"
#include <iostream>
#include <fstream>
#include <sstream>
void FileReader::read_file(string filename, vector< vector <int> > & input_data)
{
		ifstream myfile (filename.c_str());
		if (!myfile.is_open())
		{
			cerr << filename << " is not an existing file" << endl;
			throw "Unable to open input data file";
		}
		cout << "Reading file from " << filename << endl;
		
		int line_number = 0;
		string line;
		while( getline( myfile, line ) )
		{
			line_number++;
			if (int(input_data.size()) < line_number)
				input_data.resize(line_number);
		 istringstream iss( line );
		 int number;
		 vector<int> & current_line = input_data[line_number-1];
		 while( iss >> number )
		  current_line.push_back(number);
		}
		
		cout << "Closing file " << filename << endl;
		myfile.close();
		cout << "Final vector has " << input_data.size() << " rows" << endl;
		if (input_data.size() > int(0))
		{
			unsigned int amount_of_elements = input_data[0].size();
			for (unsigned int i = 1 ; i < input_data.size() ; i++)
				if (input_data[i].size() != amount_of_elements)
					throw "Not every row have the same amount of elements";
			cout << "Each of them has " << input_data[0].size() << " elements" <<endl;
		}
}
